package com.fzu.mmdbproxyframework.init;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.fzu.mmdbproxyframework.configurationmanager.MMDBConfigRead;
import com.fzu.mmdbproxyframework.configurationmanager.RuleConfigRead;
import com.fzu.mmdbproxyframework.configurationmanager.SourceDBConfigRead;
import com.fzu.mmdbproxyframework.model.MMDataNodeCluster;
import com.fzu.mmdbproxyframework.multitask.Failover;

public class ClusterInit {
	public MMDataNodeCluster cluster;
	public 	List<Map<String,String>> ruleParamList =new  LinkedList<Map<String,String>>();
	public 	List<Map<String,String>> dataSourceParamList =new  LinkedList<Map<String,String>>();
	public Map<String,String> SourceDBParam = new HashMap<String, String>();
	public 	Map<String,String> MMDBParam =new HashMap<String, String>();
	public List<String> tableNameList = new LinkedList<String>();
	/**
	 * @param cluster
	 */
	public ClusterInit(MMDataNodeCluster cluster) {
		super();
		this.cluster = cluster;
	}

	public  void setParam(){
//		//Spring加载bean
//		ApplicationContext Context;
//		Context =  new ClassPathXmlApplicationContext("spring-core.xml");
		//------------构建规则
	
//		Map<String,String> heartbeatmap = new HashMap<String, String>();
//		Map<String,String> Servermap = new HashMap<String, String>();
//		heartbeatmap.put("name", "heartbeat");
//		heartbeatmap.put("keepaliveTime", "50000");
//		heartbeatmap.put("createTableStr", "create table xdual(id int not null,gmt datetime,primary key (id))");
//		heartbeatmap.put("insertStr", "insert into xdual values(1,now());");
//		heartbeatmap.put("updateStr", "update xdual set gmt=now() where id=1");
//		heartbeatmap.put("selectStr", "select 1 from xdual");
//		heartbeatmap.put("dropTableIfExitStr", "drop table if exists xdual");
//		Servermap.put("name", "Server");
//		Servermap.put("updatePeriod", "10000");
//		Servermap.put("Master_Slave", "true");
//		Servermap.put("SourceDBNodeType", "mysql");
//		Servermap.put("MMDBType", "h2");
//		Servermap.put("MMDBNumbers", "2");
//		Servermap.put("AllTables", "false");
//		ruleParamList.add(heartbeatmap);
//		ruleParamList.add(Servermap);
		new RuleConfigRead(cluster).buildRuleList(ruleParamList);
		
		
		//------------构建数据源
//	
//		SourceDBParam.put("type", "mysql");
//		SourceDBParam.put("name", "DBNodeName");
//		SourceDBParam.put("mode", "Source");
//	
//		//有两个就要配置两个
//		Map<String,String> dataSourcemap = new HashMap<String, String>();
//		dataSourcemap.put("dataSource_name", "dataSource_name");
//		dataSourcemap.put("dataSource_type", "master");
//		dataSourcemap.put("dataSource_dbType", "mysql");
//		dataSourcemap.put("dataSource_dbMode", "update");
//		dataSourcemap.put("dataSource_url", "jdbc:mysql://127.0.0.1:3306/examdb");
//		dataSourcemap.put("dataSource_username", "root");
//		dataSourcemap.put("dataSource_password", "root");
//		dataSourceParamList.add(dataSourcemap);
		new SourceDBConfigRead(cluster).buildSourceDB(SourceDBParam, dataSourceParamList);
		
		//构建mmdb配置
//
//		MMDBParam.put("name", "mm_DB_1");
//		MMDBParam.put("type", cluster.getConfigContext().getMMDBType());
//		MMDBParam.put("mode", "MEM");
//		MMDBParam.put("dataSource_name", "mm_ds_m");
//		MMDBParam.put("dataSource_type", "master");
//		MMDBParam.put("dataSource_dbType", cluster.getConfigContext().getMMDBType());
//		MMDBParam.put("dataSource_dbMode", "MEM");
//
//		tableNameList.add("user_comment");
//		tableNameList.add("push_history");
		System.out.println("MMDBParam"+MMDBParam);
		new MMDBConfigRead(cluster).buildMMDBConfig(MMDBParam, tableNameList);
	}
	
	public void buildMMDBClusterEnvironment() throws SQLException, IOException{
	
		cluster.getClusterManager().createDataNode_SetTableToDataNode();
		cluster.getClusterManager().createVMByMMDataNode();
		//启动各个虚拟机中的h2DB数据库
		cluster.getClusterManager().startMMDataNodeServer();
		cluster.getClusterManager().createTableInAllMMDataNode();
		cluster.getClusterManager().transferDataToAllMMDBDataNode();
		System.out.println("=====构建成功==");
	
		
	}

	public void initHeartBeat() throws SQLException{
		new Failover(cluster).creatHeartbeatTableInALLMMDB();
	}

	public List<Map<String, String>> getRuleParamList() {
		return ruleParamList;
	}

	public void setRuleParamList(List<Map<String, String>> ruleParamList) {
		this.ruleParamList = ruleParamList;
	}

	public List<Map<String, String>> getDataSourceParamList() {
		return dataSourceParamList;
	}

	public void setDataSourceParamList(List<Map<String, String>> dataSourceParamList) {
		this.dataSourceParamList = dataSourceParamList;
	}

	public Map<String, String> getSourceDBParam() {
		return SourceDBParam;
	}

	public void setSourceDBParam(Map<String, String> sourceDBParam) {
		SourceDBParam = sourceDBParam;
	}

	public Map<String, String> getMMDBParam() {
		return MMDBParam;
	}

	public void setMMDBParam(Map<String, String> mMDBParam) {
		MMDBParam = mMDBParam;
	}

	public List<String> getTableNameList() {
		return tableNameList;
	}

	public void setTableNameList(List<String> tableNameList) {
		this.tableNameList = tableNameList;
	}

	public MMDataNodeCluster getCluster() {
		return cluster;
	}
	

	
}
